<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\Coupon as CouponModel;
use app\common\model\ActionLog;

class Coupon extends Admin {
  protected $Coupon;
  protected $CouponList;
  public function _initialize() {
		parent::_initialize();
    $this->Coupon = model('Coupon');
    $this->CouponList = model('CouponList');
	}
  public function index() {
    $map  = array('status' => array('gt', -1));
    $list = $this->Coupon->where($map)->order('sort asc,id desc')->paginate(10);

    Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->assign('list', $list);
		$this->setMeta('优惠券管理');
		return $this->fetch();
	}

  public function add() {
    if (IS_POST) {
      	$result = $this->Coupon->change();
      if($result){
        $this->success('添加成功！',  url('admin/Coupon/index'));
      }else{
        $this->error('添加失败！');
      }
    } else {
			$data = array(
        'info'    => array('status' => 1,'sort'=>1),
				'keyList' => $this->Coupon->addfield,
			);
			$this->assign($data);
			$this->setMeta("添加分类");
			return $this->fetch('public/edit');
		}
  }

  public function edit($id) {
		if (!$id) {
			return $this->error("非法操作！");
		}
		if (IS_POST) {
			$result = $this->Coupon->change();
			if ($result) {
				return $this->success("编辑成功！", url('admin/Coupon/index'));
			} else {
				return $this->error("编辑失败！");
			}
		} else {
			$info = $this->Coupon->where(array('id' => $id))->find();
			$data = array(
				'info'    => $info,
				'keyList' => $this->Coupon->addfield,
			);
			$this->assign($data);
			$this->setMeta('编辑分类');
			return $this->fetch('public/edit');
		}
	}

  public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = $this->Coupon->where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}


  public function lists($id){
    if (!$id) {
			return $this->error("非法操作！");
		}
    $list = $this->CouponList->where(['coupon_id'=>$id])->order('id desc')->paginate(10);
    // $date= strtotime('2038-01-01');
    // $this->CouponList->where(['coupon_id'=>2])->update(['deadline'=>$date]);
    $this->assign('coupon_id', $id);
		$this->assign('list', $list);
		$this->setMeta('优惠码列表');
		return $this->fetch();
  }


public function addcode($id) {
    if (!$id) {
      return $this->error("非法操作！");
    }
  if (IS_POST) {
    $num        = input('post.num');
    $coupon_id       = input('post.coupon_id');
    $deadline   = input('post.deadline');

    if($num<1){
      $this->error('数量填写错误！');
    }
    $info  =  $this->Coupon->find($coupon_id);

    $timePrefix = date('md');
    for ($i=0; $i < $num ; $i++) {
      $data[$i]['code']  = $timePrefix.rand_number(4);
      $data[$i]['coupon_id']  = $coupon_id;
      $data[$i]['price'] = $info['reduce_prcie'];
      $data[$i]['total'] = $info['total_price'];
      $data[$i]['deadline']  = $deadline;
    }

    $result = $this->CouponList->saveAll($data);
    if($result){
      $this->success('添加成功！',  url('admin/Coupon/lists',array('id'=>$id)));
    }else{
      $this->error('添加失败！');
    }
  } else {
    $data = array(
      'info'    => array('coupon_id' => $id),
      'keyList' => $this->Coupon->addcode,
    );

    $this->assign($data);
    $this->setMeta("生成优惠码");
    return $this->fetch('public/edit');
  }
}

public function delcode() {
  $id = $this->getArrayParam('id');
  if (empty($id)) {
    return $this->error("非法操作！");
  }
  $result = $this->CouponList->where(array('id' => array('IN', $id)))->delete();
  if ($result) {
    return $this->success("删除成功！");
  } else {
    return $this->error("删除失败！");
  }
}


public function integralhistory($keyword=null){
  if($keyword){
    $idarr = db('WxUser')->where('nickname','like','%'.$keyword.'%')->column('id');
  }



  if($idarr){
    $list = ActionLog::where(['action_id'=>2])->where('user_id','in',$idarr)->order('id desc')->paginate(20);
  }else{
    $list = ActionLog::where(['action_id'=>2])->order('id desc')->paginate(20);
  }


  $this->assign('list', $list);
  $this->setMeta('兑换记录');
    return $this->fetch();
}

public function getUserList($val){
    $map['id|nickname|name'] = array('like', '%' . (string) $val . '%');
    $list  = model('WxUser')->where($map)->limit(20)->select();
    if($list){
        return $this->success('查询成功', '', $list);
    }else{
        return $this->error('查询成功', '', $list);
    }

}

public function setCustomer($id, $couponid){
    if($id && $couponid){
          $userInfo = model('wxUser')->find($id);
          $couponlistInfo = model('CouponList')->find($couponid);
          if(!$userInfo['phone']){
            $this->error('该用户还不是会员，没有电话信息，不能发送短信，请先注册');
          }


        $r= db('CouponList')->where(['id'=>$couponid])->update(['uid'=>$id]);




        if($r){
          //发送短信给用户
          sendcomSms('SMS_132465043', ['price'=>(string)$couponlistInfo['price']], (string)$userInfo['phone']);
            $this->success('操作成功');
        }else{
            $this->error('操作错误');
        }
    }else{
        $this->error('参数错误');
    }

}




}

 ?>
